package com.mohe.shanpao.lucene;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class IndexFiles {

	public static void main(String[] args) {
		String indexPath = "index";
		String docsPath = "doc";
		boolean create = true;
		
		final Path docDir = Paths.get(docsPath);
		final Path indexDir = Paths.get(indexPath);
		
		if(Files.isReadable(docDir))
		{
			try 
			{
				System.out.println("Indexing to directory '" + indexPath + "'...");
				Directory dir = FSDirectory.open(indexDir);
				Analyzer analyzer = new StandardAnalyzer();
				
				IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
				
				if(create)
				{
					iwc.setOpenMode(OpenMode.CREATE);
				}
				else
				{
					iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
				}
				
//				iwc.setRAMBufferSizeMB(256.0);
				
				IndexWriter writer = new IndexWriter(dir,iwc);
				indexDocs(writer,docDir);
				
				writer.close();
				
				
			} 
			catch (Exception e) 
			{
				System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
			}
		}
	}
	
	
	static void indexDocs(final IndexWriter writer,Path doc) throws IOException
	{
		if(Files.isDirectory(doc))
		{
			Files.walkFileTree(doc, new SimpleFileVisitor<Path>(){
				@Override
				public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
					try {
						indexDoc(writer, file, attrs.lastModifiedTime().toMillis());
					} catch (Exception e) {
					}
					
					return FileVisitResult.CONTINUE;
				}
				
			});
		}
		else
		{
			indexDoc(writer,doc,Files.getLastModifiedTime(doc).toMillis());
		}
	}
	
	
	static void indexDoc(IndexWriter writer, Path file, long lastModified)
	{
		try(InputStream stream = Files.newInputStream(file)) {
			Document document = new Document();
			
			Field pathField = new StringField("path",file.toString(), Field.Store.YES);
			
			document.add(pathField);
			
			document.add(new LongPoint("modified", lastModified));
			
			document.add(new TextField("contents",new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
			
			if(writer.getConfig().getOpenMode() == OpenMode.CREATE)
			{
				System.out.println("adding " + file);
				writer.addDocument(document);
			}
			else
			{
				System.out.println("updating " + file);
				writer.updateDocument(new Term("path", file.toString()), document);
			}
			
		} catch (Exception e) {
		}
	}
}
